#include <windows.h>
//#include <stdlib.h>
//#include <string.h>
#include <tchar.h>
#include "Graph.h"
#include "resource.h"

static TCHAR szWindowClass[] = _T("win32app");
static TCHAR szTitle[] = _T("Simplest windows app");
HINSTANCE hInst;

static HBITMAP CreateBitmapFromPixels( HDC hDC, UINT uWidth, UINT uHeight, UINT uBitsPerPixel, LPVOID pBits)
    {
           if(uBitsPerPixel < 8) // NOT IMPLEMENTED YET
              return NULL;
 
          HBITMAP hBitmap = 0;
          if ( !uWidth || !uHeight || !uBitsPerPixel )
             return hBitmap;
          LONG lBmpSize = uWidth * uHeight * (uBitsPerPixel/8) ;
          BITMAPINFO bmpInfo = { 0 };
          bmpInfo.bmiHeader.biBitCount = uBitsPerPixel;
          bmpInfo.bmiHeader.biHeight = uHeight;
          bmpInfo.bmiHeader.biWidth = uWidth;
          bmpInfo.bmiHeader.biPlanes = 1;
          bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
               // Pointer to access the pixels of bitmap
          UINT * pPixels = 0;
          hBitmap = CreateDIBSection( hDC, (BITMAPINFO *)&
                                      bmpInfo, DIB_RGB_COLORS, (void **)&
                                      pPixels , NULL, 0);
 
          if ( !hBitmap )
              return hBitmap; // return if invalid bitmaps

          //SetBitmapBits( hBitmap, lBmpSize, pBits);
          // Directly Write
          memcpy(pPixels, pBits, lBmpSize );
 
          return hBitmap;
    }

void DoDrawing(HDC hdc)
{
	int w = 800;
	int h = 700;
	Graph g(w, h);
	for (int y = 00; y < h; y++)
	{
		for (int x = 0; x < w; x++)
		{
			g.PutPixel(x, y, x*2+x*256+y*256*256);
		}
	}

	HDC hSrcDC = CreateCompatibleDC(hdc);
	HBITMAP bmp = CreateBitmapFromPixels(hdc, w, h, 32, g.GetBuffer());
	HGDIOBJ oldObject = SelectObject(hSrcDC, bmp);
	BitBlt(hdc, 20, 30, w, h, hSrcDC, 0, 0, SRCCOPY);
	SelectObject(hSrcDC, oldObject);
	DeleteDC(hSrcDC);
	DeleteObject(bmp);
	//delete [] pBytes;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HRSRC rc = FindResource(NULL, MAKEINTRESOURCE(IDR_ASTEROID1), _T("ASTEROID"));

    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("Hello, World!");

    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);

        // Here your application is laid out.
        // For this introduction, we just print out "Hello, World!"
        // in the top left corner.
        //TextOut(hdc, 5, 5, greeting, _tcslen(greeting));
		BitBlt(hdc, 0, 0, 600, 600, NULL, 0, 0, BLACKNESS);
		DoDrawing(hdc);
        //TODO: See BltTest. We need SetBitmapBits for fast operation.

        // End application specific layout section.

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application dows not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        600, 500,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}
